package com.atguigu.tingshu.payment.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.service.KafkaService;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.order.OrderDetail;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;

import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.model.payment.PaymentInfo;
import com.atguigu.tingshu.payment.mapper.PaymentInfoMapper;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wechat.pay.java.service.payments.model.Transaction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@SuppressWarnings({"unchecked", "rawtypes"})
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {
	@Autowired
	private PaymentInfoMapper paymentInfoMapper;
	@Autowired
	private KafkaService kafkaService;

	@Override
	public void savePaymentInfo(OrderInfo orderInfo) {
		// 已经保存的不再次保存
		String orderNo = orderInfo.getOrderNo();
		PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new LambdaQueryWrapper<PaymentInfo>().eq(PaymentInfo::getOrderNo, orderNo));
		// 如果交易信息已经保存,就不再次保存了
		if (Objects.nonNull(paymentInfo))
			return;
		List<OrderDetail> orderDetailList = orderInfo.getOrderDetailList();
		String itemName = orderDetailList.get(0).getItemName();
		paymentInfo = new PaymentInfo();
		paymentInfo.setUserId(orderInfo.getUserId());
		paymentInfo.setPaymentType(SystemConstant.PAYMENT_TYPE_ORDER);
		paymentInfo.setOrderNo(orderNo);
		paymentInfo.setPayWay(orderInfo.getPayWay());
		paymentInfo.setOutTradeNo(orderNo);
		paymentInfo.setAmount(orderInfo.getOrderAmount());
		paymentInfo.setContent(itemName);
		paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);
		// 	保存交易信息
		paymentInfoMapper.insert(paymentInfo);
	}

	@Override
	public void updatePaymentStatus(Transaction transaction) {
		String orderNo = transaction.getOutTradeNo();
		PaymentInfo paymentInfo = this.getOne(new LambdaQueryWrapper<PaymentInfo>().eq(PaymentInfo::getOrderNo, orderNo));
		// 如果已经是已支付就直接返回
		if(paymentInfo.getPaymentStatus().equals(SystemConstant.PAYMENT_STATUS_PAID)) return;
		// 更新订单状态
		paymentInfo.setCallbackContent(JSON.toJSONString(transaction));
		paymentInfo.setCallbackTime(new Date());
		paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_PAID);  // 已支付
		this.updateById(paymentInfo);

		switch (paymentInfo.getPaymentType()){
			case SystemConstant.PAYMENT_TYPE_ORDER -> {
				log.info("交易信息的支付状态修改完毕,发送更改订单信息的消息....orderNo: {}",orderNo);
				kafkaService.sendMsg(KafkaConstant.QUEUE_ORDER_PAY_SUCCESS,orderNo);
				log.info("交易信息的支付状态修改完毕,发送更改订单信息的消息成功....orderNo: {}",orderNo);
			}
			case SystemConstant.PAYMENT_TYPE_RECHARGE -> {
				log.info("充值信息的支付状态修改完毕,发送更改充值信息的消息....orderNo: {}",orderNo);
				kafkaService.sendMsg(KafkaConstant.QUEUE_RECHARGE_PAY_SUCCESS,orderNo);
				log.info("充值信息的支付状态修改完毕,发送更改充值信息的消息成功....orderNo: {}",orderNo);
			}
		}
	}

	@Override
	public void savePaymentInfo(RechargeInfo rechargeInfo) {
		// 已经保存的不再次保存
		String orderNo = rechargeInfo.getOrderNo();
		PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new LambdaQueryWrapper<PaymentInfo>().eq(PaymentInfo::getOrderNo, orderNo));
		// 如果充值信息已经保存,就不再次保存了
		if (Objects.nonNull(paymentInfo))
			return;

		paymentInfo = new PaymentInfo();
		paymentInfo.setUserId(rechargeInfo.getUserId());
		paymentInfo.setPaymentType(SystemConstant.PAYMENT_TYPE_RECHARGE);
		paymentInfo.setOrderNo(orderNo);
		paymentInfo.setPayWay(rechargeInfo.getPayWay());
		paymentInfo.setOutTradeNo(orderNo);
		paymentInfo.setAmount(rechargeInfo.getRechargeAmount());
		paymentInfo.setContent("充值");
		paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);
		// 	保存交易信息
		paymentInfoMapper.insert(paymentInfo);
	}
}
